<?php

class Infinity_Bestseller_Block_List extends Mage_Catalog_Block_Product_List {

    const DEFAULT_CUR_PAGE = '1';
    const DEFAULT_PAGE_SIZE = '5';
    const DEFAULT_ORDER_DIR = 'DESC';

    static $_curpage;
    static $_order_dir;

    protected function _getProductCollection()
    {
        parent::__construct();

        $storeId = Mage::app()->getStore()->getId();
        $products = Mage::getResourceModel('reports/product_collection')
                ->addAttributeToSelect('*')
                ->addOrderedQty()
                ->setStoreId($storeId)
                ->addStoreFilter($storeId)
                ->setPageSize(self::DEFAULT_PAGE_SIZE)
                ->setOrder('ordered_qty', $this->getOrderDir())
                ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id=entity_id', null, 'left'
                )
                ->setCurPage($this->getCurPage());

        if (Mage::getStoreConfig('bestseller/infinity_bestseller_config_enabled/max_product')) {
            $products->setPageSize(Mage::getStoreConfig('bestseller/infinity_bestseller_config_enabled/max_product'));
        }

        $categoryEnable = Mage::getStoreConfig('bestseller/infinity_bestseller_products_group_category/enable');
        $categorySting = Mage::getStoreConfig('bestseller/infinity_bestseller_products_group_category/categories');
        if ($categoryEnable == 1 && $categorySting != null) {
            $categoryArr = explode(',', $categorySting);
            $products->addAttributeToFilter('category_id', array('in' => $categoryArr));
        }

        $productFlatData = Mage::getStoreConfig('catalog/frontend/flat_catalog_product');

        if ($productFlatData == "1") {
            $products->getSelect()->joinLeft(
                    array('flat' => 'catalog_product_flat_' . $storeId), "(e.entity_id = flat.entity_id ) ", array(
                'flat.name AS name', 'flat.small_image AS small_image', 'flat.price AS price', 'flat.special_price as special_price', 'flat.special_from_date AS special_from_date', 'flat.special_to_date AS special_to_date'
                    )
            );
        }

        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
        Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($products);

        $this->_productCollection = $products;

        return $this->_productCollection;
    }

    public function setCurPage($count)
    {
        self::$_curpage = $count;
        return $this;
    }

    public function getCurPage()
    {
        if (null === self::$_curpage) {
            self::$_curpage = self::DEFAULT_CUR_PAGE;
        }
        return self::$_curpage;
    }

    public function setOrderDir($order)
    {
        self::$_order_dir = $order;
        return $this;
    }

    public function getOrderDir()
    {
        if (null === self::$_order_dir) {
            self::$_order_dir = self::DEFAULT_ORDER_DIR;
        }
        return self::$_order_dir;
    }

    public function getToolbarHtml()
    {
        
    }

}

?>